﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GXRTBTC.PublicClassLibrary.Model
{
    /// <summary>
    /// 数据分页查询结果
    /// </summary>
    public class PageResult
    {
        /// <summary>
        /// 获取结果
        /// </summary>
        public bool result { get; set; } = true;
        /// <summary>
        /// 提示信息
        /// </summary>
        public string message { get; set; } = "";
        /// <summary>
        /// 用时
        /// </summary>
        public long time { get; set; } = 0;
        /// <summary>
        /// 分页信息
        /// </summary>
        public Page page { get; set; }
        private object _datas { get; set; } = null;
        /// <summary>
        /// 数据
        /// List<Dictionary<string, string>>
        /// </summary>
        public object datas 
        { 
            get
            {
                return _datas;
            }
            set
            {
                _datas = value;
                page.totalCount = ((List<Dictionary<string, string>>)value).Count;
            }
        }
    }

    /// <summary>
    /// 分页信息
    /// </summary>
    public class Page
    {
        /// <summary>
        /// 是否分页
        /// </summary>
        public bool paging { get; set; } = true;
        /// <summary>
        /// 当前页码
        /// </summary>
        private int _pageIndex { get; set; } = 1;
        public int pageIndex
        {
            get
            {
                return paging ? _pageIndex : 1;
            }
            set
            {
                _pageIndex = value;
            }
        }

        /// <summary>
        /// 每页行数
        /// 如果是一个很大的值，代表不分页的意思
        /// </summary>
        private int _pageSize { get; set; } = 20;
        public int pageSize
        {
            get
            {
                return paging ? _pageSize : totalCount;
            }
            set
            {
                _pageSize = value;
            }
        }

        /// <summary>
        /// 当前页码记录范围
        /// </summary>
        public string pageRange 
        {
            get 
            {
                int totalPages = this.totalPages;
                if (!paging)
                {
                    return "1-" + totalCount;
                }
                else if (pageIndex > totalPages)
                {
                    return "0-0";
                }
                else
                {
                    int start = ((pageIndex - 1) * pageSize) + 1;
                    int end = (pageIndex == totalPages ? totalCount : pageIndex * pageSize);
                    return start + "-" + end;
                }
            }
        }

        /// <summary>
        /// 记录数
        /// </summary>
        public int recordCount 
        {
            get
            {
                return paging ? (pageIndex < totalPages ? pageSize : (pageIndex == totalPages ? (totalCount - ((pageIndex - 1) * pageSize)) : 0)) : totalCount;
            }
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int totalCount { get; set; } = 0;

        /// <summary>
        /// 总页数
        /// </summary>
        public int totalPages
        {
            get
            {
                int tmpTotalPages = totalCount / (pageSize > 0 ? pageSize : 1);
                int mod = totalCount % (pageSize > 0 ? pageSize : 1);
                if (mod > 0)
                {
                    tmpTotalPages = tmpTotalPages + 1;
                }

                return paging ? tmpTotalPages : 1;
            }
        }
    }
}
